IPython-Unittest / Jupyter Dojo

Testes no Jupyter usando cell magics

João Felipe Nicolaci Pimentel (joaofelipenp@gmail.com)

conda install -c conda-forge jupyter_dojo

Cell Magics + Toolbar

Ou

pip install ipython_unittest

Apenas cell magics

https://github.com/JoaoFelipe/ipython-unittest


In [3]:
%load_ext ipython_unittest

In [7]:
def add(x, y):
    return x + y

In [8]:
%%unittest
assert add(1, 1) == 2
assert add(1, 2) == 3
assert add(2, 2) == 4


...
----------------------------------------------------------------------
Ran 3 tests in 0.004s

OK
Out[8]:
<unittest.runner.TextTestResult run=3 errors=0 failures=0>

In [ ]:


In [ ]:
%load_ext ipython_unittest

In [ ]:
def fizzbuzz():
    pass

In [ ]:
%%unittest -p 1
assert fizzbuzz() == 0

In [ ]:


In [ ]:


In [ ]:
import unittest
import sys

class JupyterTest(unittest.TestCase):

    def test_add_1_1_returns_2(self):
        self.assertEqual(add(1, 1), 2)

    def test_add_1_2_returns_3(self):
        self.assertEqual(add(1, 2), 3)

    def test_add_2_2_returns_4(self):
        self.assertEqual(add(2, 2), 4)
        
suite = unittest.TestLoader().loadTestsFromTestCase(JupyterTest)
unittest.TextTestRunner(verbosity=1, stream=sys.stdout).run(suite)

In [ ]:


In [ ]:
!pip install astunparse

In [9]:
%%unittest -u
"add 1 + 1 returns 2"
assert add(1, 1) == 2
"add 1 + 2 returns 3"
assert add(1, 2) == 3
"add 2 + 2 returns 4"
assert add(2, 2) == 4



class JupyterTest(unittest.TestCase):

    def test_add_1_1_returns_2(self):
        'add 1 + 1 returns 2'
        self.assertEqual(add(1, 1), 2)

    def test_add_1_2_returns_3(self):
        'add 1 + 2 returns 3'
        self.assertEqual(add(1, 2), 3)

    def test_add_2_2_returns_4(self):
        'add 2 + 2 returns 4'
        self.assertEqual(add(2, 2), 4)

...
----------------------------------------------------------------------
Ran 3 tests in 0.009s

OK
Out[9]:
<unittest.runner.TextTestResult run=3 errors=0 failures=0>

In [ ]:


In [ ]:


In [ ]:


In [ ]:

Other magics:


In [ ]:
%%write javascript test.js
var assert = require('assert');
describe('Array', function() {
  describe('#indexOf()', function() {
    it('should return -1 when the value is not present', function() {
      assert.equal(-1, [1,2,3].indexOf(4));
    });
  });
});

In [ ]:
%%external --previous 1
mocha test.js

In [ ]:
%%unittest_main
class JupyterTest(unittest.TestCase):

    def test_add_1_1_returns_2(self):
        self.assertEqual(add(1, 1), 2)

    def test_add_1_2_returns_3(self):
        self.assertEqual(add(1, 2), 3)

    def test_add_2_2_returns_4(self):
        self.assertEqual(add(2, 2), 4)

In [ ]:
%%unittest_testcase
def test_add_1_1_returns_2(self):
    self.assertEqual(add(1, 1), 2)

def test_add_1_2_returns_3(self):
    self.assertEqual(add(1, 2), 3)

def test_add_2_2_returns_4(self):
    self.assertEqual(add(2, 2), 4)

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:

IPython-Unittest / Jupyter Dojo

Testes no Jupyter usando cell magics

João Felipe Nicolaci Pimentel (joaofelipenp@gmail.com)

conda install -c conda-forge jupyter_dojo

Cell Magics + Toolbar

Ou

pip install ipython_unittest

Apenas cell magics

https://github.com/JoaoFelipe/ipython-unittest


In [ ]: